package com.towery.dbsrcmanager.jobs;

/**
 * Created by User on 2017/2/17.
 */

import com.towery.dbsrcmanager.db.service.VersionService;
import com.towery.dbsrcmanager.mybatis.base.dao.TDatabaseDAO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

/**
 * 定时任务配置类
 *
 */
@Configuration
@Slf4j
@EnableScheduling // 启用定时任务
public class SchedulingConfig {

    @Autowired
    TDatabaseDAO databaseDAO;
    @Autowired
    VersionService versionService;

    @Scheduled(cron = "0 0 10 * * ?") // 每20秒执行一次
    public void scheduler() {
        log.info(">>>>>>>>>>>>> scheduled ... ");

        List<Map<String,Object>> list = null;
        try {
            list = databaseDAO.getDatabaseTreeList();
        } catch (Exception e) {
            log.error("",e);
        }
        if(list!=null && list.size()>0){
            int i=0;
            int s =list.size();
            for(Map<String,Object> d: list){
                String databaseId =(String)d.get("id");
                try {
                    boolean submit =(i==s-1);
                    versionService.sync(databaseId,submit);
                }catch(Exception e){
                    log.error("",e);
                }
                i++;
            }
        }
    }

}
